package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.domain.BusStatement;
import cn.wolfcode.business.domain.BusStatementItem;
import cn.wolfcode.business.domain.vo.StatementItemVO;
import cn.wolfcode.business.mapper.BusStatementItemMapper;
import cn.wolfcode.business.service.AppointmentService;
import cn.wolfcode.business.service.BusinessStatementService;
import cn.wolfcode.business.service.IBusStatementItemService;
import cn.wolfcode.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 结算单明细Service业务层处理
 * 
 * @author wolfcode
 * @date 2025-09-23
 */
@Service
@Transactional
public class BusStatementItemServiceImpl implements IBusStatementItemService 
{
    @Autowired
    private BusStatementItemMapper busStatementItemMapper;
    @Autowired
    private BusinessStatementService businessStatementService;
    @Autowired
    private AppointmentService appointmentService;

    @Override
    public int save(StatementItemVO vo) {
        if (vo==null||vo.getDiscountPrice()==null||vo.getStatementItemList()==null||
                vo.getStatementItemList().isEmpty()){
            throw new RuntimeException("结算单明细不能为空");
        }
        Long statementId = vo.getStatementItemList().get(0).getStatementId();
        BusStatement busStatement = businessStatementService.selectStatementById(statementId);
        if (busStatement==null||busStatement.getStatus()==null){
            throw new RuntimeException("结算单不存在");
        }
        if (BusStatement.STATUS_PAID.equals(busStatement.getStatus())){
            throw new RuntimeException("结算单只有在消费中才能被保存");
        }
        //删除旧的数据
        busStatementItemMapper.deleteRelationStatementId(statementId);
        //总价格
        BigDecimal totalAmount = new BigDecimal(0);
        //总数量
        BigDecimal totalQuantity = new BigDecimal(0);
        //总折扣
        BigDecimal discountAmount = vo.getDiscountPrice();
        for(BusStatementItem item:vo.getStatementItemList()){
            totalAmount = totalAmount.add(item.getItemPrice().multiply(new BigDecimal(item.getItemQuantity())));
            totalQuantity = totalQuantity.add(new BigDecimal(item.getItemQuantity()));
        }
        if (!(totalAmount.compareTo(new BigDecimal("0"))>=0
                && discountAmount.compareTo(new BigDecimal("0"))>=0
                && totalAmount.compareTo(discountAmount)>=0)){
            throw new RuntimeException("价格或折扣不合法");
        }
        if (totalQuantity.compareTo(new BigDecimal("0"))<=0){
            throw new RuntimeException("数量不合法");
        }
        businessStatementService.updateAmount(statementId,totalAmount,totalQuantity,discountAmount);
        return busStatementItemMapper.insert(vo.getStatementItemList());
    }

    @Override
    public List<BusStatementItem> list(Long statementId) {
        BusStatementItem busStatementItem = new BusStatementItem();
        busStatementItem.setStatementId(statementId);
        return busStatementItemMapper.selectBusStatementItemList(busStatementItem);
    }

    @Override
    @Transactional
    public int pay(Long statementId) {
        if (statementId==null){
            throw new RuntimeException("结算单id不能为空");
        }
        BusStatement busStatement = businessStatementService.selectStatementById(statementId);
        if (busStatement==null){
            throw new RuntimeException("结算单不存在");
        }
        if(BusStatement.STATUS_PAID.equals(busStatement.getStatus())){
            throw new RuntimeException("结算单已支付");
        }
        Long payId = SecurityUtils.getUserId();

        if (busStatement.getAppointmentId()!=null){
            appointmentService.updateStatus(busStatement.getAppointmentId(), BusAppointment.STATUS_PAID);
}
        return businessStatementService.updatePayStatus(statementId,payId,new Date(),BusStatement.STATUS_PAID);
    }
}
